package com.cslg.ac.demo.service.auth.phone;

import com.cslg.ac.demo.client.JsonReqClient;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.dao.user.team.TeamInfoDAO;
import com.cslg.ac.demo.dao.user.UserAuthDAO;
import com.cslg.ac.demo.dao.user.UserDAO;
import com.cslg.ac.demo.dao.user.po.UserAuthPO;
import com.cslg.ac.demo.dao.user.po.UserPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PhoneService {
    @Autowired
    JsonReqClient jsonReqClient;
    @Autowired
    UserAuthDAO userAuthDAO;
    @Autowired
    UserDAO userDAO;
    @Autowired
    TeamInfoDAO teamInfoDAO;
    @Autowired
    PersonInfoDAO personInfoDAO;


    public int authPhone(String phone,String param,String token,String sid,String appid,String templateid){
        UserAuthPO userAuthPO = userAuthDAO.selectByName(phone);
        UserPO userPO = userDAO.SelectByName(phone);
        if(userAuthPO != null && userPO != null){
            return ResponseCode.USER_ALREADY_EXISTS;
        }
        if(userAuthPO != null){
            return ResponseCode.ALREADY_HAVE_AUTH;
        }
        jsonReqClient.sendSms(sid,token,appid,templateid,param,phone,"");
        return ResponseCode.REQUEST_SUCCESSED;
    }

    public int authSuccess(String phone){
        //在personInfo/teamInfo里面加上手机号以及在auth里面加上验证通过

        UserAuthPO userAuthPO1 = new UserAuthPO();
        userAuthPO1.setUserName(phone);
        userAuthPO1.setEmailAuth(1);
        userAuthPO1.setPhoneAuth(0);
        userAuthPO1.setIdCardAuth(1);
        UserAuthPO userAuthPO = userAuthDAO.selectByName(phone);
        if(userAuthPO == null){
            userAuthDAO.insert(userAuthPO1);
            return ResponseCode.REQUEST_SUCCESSED;
        }
        if(userAuthPO.getPhoneAuth() == 0){
            return ResponseCode.ALREADY_HAVE_AUTH;//已经激活过了
        }
        return ResponseCode.SERVER_ERROR;

    }

}
